package com.fyy.controller;

import com.fyy.model.User;
import com.fyy.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author fyy
 * @Description
 * @Date 2020/5/26 9:44
 */
@RestController
@RequestMapping(value = "/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/add")
    public String saveUser(String username,String pass){
        User user = new User();
        user.setUsername(username).setPass(pass);
        boolean result = userService.addUser(user);
        if(result){
            return "插入成功";
        }
        return "插入失败";
    }

    @GetMapping("/query/{id}")
    public String queryUserById(@Param(value = "id")Long id){
        User user = userService.queryUserById(id);
        return user.toString();
    }

    @GetMapping("/query")
    public Object queryAllUser(){
        return userService.queryUserList();
    }

    @GetMapping("/discover")
    public Object client(){
       List<String> services =  client.getServices();
       System.out.println("DiscoveryClient=>services:" + services);

       List<ServiceInstance> instances = client.getInstances("PEO8081");
       for (ServiceInstance instance : instances) {
           System.out.println("主机:" + instance.getHost());
           System.out.println("端口:" + instance.getPort());
           System.out.println("地址:" + instance.getUri());
           System.out.println("服务:" + instance.getServiceId());
       }
        return this.client;
    }


}
